<template>
  <div>
    <!-- table -->
    <BreakSpace label="旧件销毁申请" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel" :list="formList" :cols="4" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <BreakSpace label="旧件列表" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'records'"
      :onColumnsChange="(columns) => (this.columns = columns)"
      :onCellChange="onCellChange"
      :isSelectColumn="false"
      height="auto"
      :isPagination=false
    >
    </FilterTable>
    <div style="padding-top: 3rem"></div>
    <!-- <FormPanel ref="formPanel1" :list="formList1" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandleFile" /> -->
    <!-- <div style="padding-top: 3rem"></div> -->
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right',
      }"
    >
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
    <BaseDialog :visible.sync="visibleList" :title="'旧件列表'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <usedPartsList @close="closeAdd" @save="saveAdd"></usedPartsList>
    </BaseDialog>
  </div>
</template>
<script>
import usedPartsList from './usedPartsList.vue';
//字典
import { getPartType } from '@/api/dictionary/index';
//手机号校验
import { phone } from '@/utils/validate';
import { updateByPtId , applyDestory } from '@/api/usedPartsDestructionDealer/usedPartsDestroyedApply';

export default {
  components: {
    usedPartsList,
  },
  props: ['editRow'],
  // mixins: [dictionary],
  data() {
    return {
      dataList: [],
      dataList1: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      saveData: null,
      activeName: '审核记录',
      saveDataFile: {},
      visibleList: false,
    };
  },
  created() {
    this.assignment();
  },
  mounted() {
    let list = Object.assign([], this.editRow.dtlList);
    list.map(x => {
      x.claimNo = x.cmsClaimNo;
      x.pestructionCount = x.partCount;
      x.partCount = x.claimPartCount;
      x.claimId = x.cmsClaimId;
    });
    this.$refs.table.$refs.pageTable.EXECUTE_INSERT(list);
  },
  methods: {
    async assignment() {
      let res = await getPartType({ type: 5531 });
      for (let row in this.editRow) {
        this.formList.map((x) => {
          if (row == 'fileList' && x.fieldName == 'wayFiles') {
            x.initialValue = this.editRow.fileList.map((f) => ({ name: f.title, url: f.fileUrl }));
          }
          if(x.fieldName == 'destoryStatus'){
            x.initialValue = res.data.find(x => x.codeId == this.editRow.destoryStatus).codeCnDesc;
          }
          if (x.fieldName == row) {
            x.initialValue = this.editRow[row];
          }
        });
      }
    },
    onCellChange(columns, row) {
      let sum = 0;
      this.$refs.table.$refs.pageTable.list.map((x) => {
        console.log(x);
        sum += Number(x.pestructionCount);
      });
      this.formList.find((x) => x.fieldName == 'destoryCount').initialValue = sum;
      this.formList.find((x) => x.fieldName == 'cmsClaimCount').initialValue = this.$refs.table.$refs.pageTable.list.length;
    },
    add() {
      this.visibleList = true;
    },
    closeAdd() {
      this.visibleList = false;
    },
    saveAdd(val) {
      this.visibleList = false;
      let list = Object.assign([], val);
      let pageList = this.$refs.table.$refs.pageTable.list;
      val.map((x) => {
        if (pageList.find((f) => f.claimId != x.claimId)) {
          list.push(x);
        }
      });
      this.$refs.table.$refs.pageTable.EXECUTE_DELETE(this.$refs.table.$refs.pageTable.list);
      this.$refs.table.$refs.pageTable.EXECUTE_INSERT(list);
      this.formList.find((x) => x.fieldName == 'cmsClaimCount').initialValue = this.$refs.table.$refs.pageTable.list.length;
      let num = 0;
      this.$refs.table.$refs.pageTable.list.map((x) => {
        num += Number(x.pestructionCount || 0);
      });
      this.formList.find((x) => x.fieldName == 'destoryCount').initialValue = num;
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    formChangeHandleFile(val) {
      this.saveDataFile = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    deleteHandle(row) {
      this.$refs.table.$refs.pageTable.EXECUTE_DELETE([row]);
    },
    //取消操作
    closeDialog() {
      this.$emit('close');
    },
    chooseOrderNo() {
      this.visible = true;
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '销毁单号',
          fieldName: 'destoryNo',
          placeholder: '',
          readonly: true,
          disabled: true, // 置灰
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '单据状态',
          fieldName: 'destoryStatus',
          placeholder: '',
          readonly: true,
          disabled: true, // 置灰
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '索赔单数量',
          fieldName: 'cmsClaimCount',
          placeholder: '',
          readonly: true,
          disabled: true, // 置灰
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '销毁数量',
          fieldName: 'destoryCount',
          placeholder: '',
          readonly: true,
          disabled: true, // 置灰
          initialValue: '',
        },
        {
          type: 'DATE',
          label: '预计销毁日期',
          fieldName: 'destoryDate',
          placeholder: '请选择日期...',
          valueFormat: 'yyyy-MM-dd', //返回数据格式
          disabled: true, // 置灰
          rules: [{ required: true, message: '请选择日期', trigger: 'blur' }],
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '操作人',
          fieldName: 'destoryBy',
          placeholder: '请输入操作人...',
          disabled: true, // 置灰
          rules: [{ required: true, message: '请输入操作人', trigger: 'blur' }],
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '操作人电话',
          fieldName: 'destoryTel',
          placeholder: '请输入操作人电话...',
          disabled: true, // 置灰
          rules: [{required: true, message: '请确认联系电话格式', validator: phone, trigger: 'blur'}],
          initialValue: '',
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remarks',
          placeholder: '备注',
          disabled: true, // 置灰
          rows: 3,
          selfCols: 4, // 元素占据几行
          initialValue: '',
        },
        {
          type: 'UPLOAD_FILE',
          label: '上传附件',
          fieldName: 'wayFiles',
          placeholder: '上传附件...',
          rules: [{ required: true, message: '请上传文件', trigger: 'change' }],
          initialValue: [],
          disabled: true, // 置灰
          upload: {
            actionUrl: '/api/file/oss/upload',
            tipText: '支持PDF、RAR、JPG格式',
          },
        },
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
        { title: '索赔类型', dataIndex: 'cmsClaimTypeStr', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '旧件代码', dataIndex: 'partCode', sorter: true },
        { title: '旧件名称', dataIndex: 'partName', sorter: true },
        { title: '数量', dataIndex: 'partCount', sorter: true },
        { title: '销毁数量', dataIndex: 'pestructionCount', sorter: true },
        { title: '销毁说明', dataIndex: 'partRemarks', sorter: true }
      ];
    },
  },
};
</script>

<style lang="less" scoped></style>
